<?php


namespace app\admin\controller;


use app\common\library\enum\CodeEnum;

class Balance extends BaseAdmin
{
    /**
     * 资产
     *
     *
     * @return mixed
     */
    public function index()
    {
        return $this->fetch();
    }

    /**
     * 商户资产列表
     *
     *
     */
    public function getList()
    {
        $where = [];

        //组合搜索
        !empty($this->request->param('uid')) && $where['uid']
            = ['eq', $this->request->param('uid')];

        $data = $this->logicBalance->getBalanceList($where, '*', 'create_time desc', $this->request->param('limit'));

        $this->result(
            $data && !empty($data) && $data->total() > 0 ?
                [
                    'code' => CodeEnum::SUCCESS,
                    'msg' => '',
                    'count' => $data->total(),
                    'data' => $data->items()
                ] : [
                    'code' => CodeEnum::ERROR,
                    'msg' => '暂无数据',
                    'count' => 0,
                    'data' => []
                ]
        );
    }

    /**
     * 商户账户收支明细信息（仅做记录）
     *
     *
     * @return mixed
     */
    public function details()
    {
        $this->assign('uid', $this->request->param('id'));
        return $this->fetch();
    }

    /**
     * 获取商户账户收支明细信息
     *
     *
     */
    public function getDetails()
    {
        $where = [];

        //组合搜索
        $where['uid'] = ['eq', $this->request->param('uid')];

        //时间搜索  时间戳搜素
        $where['create_time'] = $this->parseRequestDate();

        $data = $this->logicBalanceChange->getBalanceChangeList($where, true, 'id desc', $this->request->param('limit'));

        $this->result(
            $data && !empty($data) && $data->total() > 0 ?
                [
                    'code' => CodeEnum::SUCCESS,
                    'msg' => '',
                    'count' => $data->total(),
                    'data' => $data->items()
                ] : [
                    'code' => CodeEnum::ERROR,
                    'msg' => '暂无数据',
                    'count' => 0,
                    'data' => []
                ]
        );
    }

    public function changeEnableMoney()
    {
        $this->result($this->logicBalance->changeEnableMoney(['id' => $this->request->param('id')], $this->request->param('value'), $this->request->param('autoReduction') == 'true'));
    }

    public function changeDisableMoney()
    {
        $this->result($this->logicBalance->changeDisableMoney(['id' => $this->request->param('id')], $this->request->param('value')));
    }


    /**
     * 打款记录(仅作记录)
     *
     *
     * @return mixed
     */
    public function paid()
    {
        return $this->fetch();
    }

    /**
     * 获取打款记录API
     *
     *
     */
    public function paidList()
    {
        $where = [];
        //组合搜索
        !empty($this->request->param('uid')) && $where['a.uid']
            = ['eq', $this->request->param('uid')];

        is_numeric($this->request->param('status')) && $where['a.status']
            = ['eq', $this->request->param('status')];

        $data = $this->logicBalanceCash->getOrderCashList($where, 'a.*,u.account,b.name as method', 'a.create_time desc', $this->request->param('limit'));

        $this->result(
            $data && !empty($data) && $data->total() > 0 ?
                [
                    'code' => CodeEnum::SUCCESS,
                    'msg' => '',
                    'count' => $data->total(),
                    'data' => $data->items()
                ] : [
                    'code' => CodeEnum::ERROR,
                    'msg' => '暂无数据',
                    'count' => 0,
                    'data' => []
                ]
        );
    }

    /**
     * 通过  提交队列后台打款
     *
     *
     *
     */
    public function deal()
    {
        $this->result($this->logicBalanceCash->pushBalanceCash(['a.id' => $this->request->param('cash_id')]));
    }


    /**
     * 驳回申请
     *
     *
     *
     */
    public function rebut()
    {
        $this->result(
            $this->logicBalanceCash->rebutBalanceCash(['a.id' => $this->request->param('cash_id')])
        );
    }


    /**
     * 打款记录(仅作记录)
     *
     *
     * @return mixed
     */
    public function settlement()
    {
        return $this->fetch();
    }

    /**
     * 获取打款记录API
     *
     *
     */
    public function settlementList()
    {
        $where = [];
        //组合搜索
        !empty($this->request->param('uid')) && $where['a.uid']
            = ['eq', $this->request->param('uid')];

        is_numeric($this->request->param('status')) && $where['a.status']
            = ['eq', $this->request->param('status')];

        $data = $this->logicBalanceSettlement->getSettlementList($where, 'a.*', 'a.update_time desc', $this->request->param('limit'));

        $this->result(
            $data && !empty($data) && $data->total() > 0 ?
                [
                    'code' => CodeEnum::SUCCESS,
                    'msg' => '',
                    'count' => $data->total(),
                    'data' => $data->items()
                ] : [
                    'code' => CodeEnum::ERROR,
                    'msg' => '暂无数据',
                    'count' => 0,
                    'data' => []
                ]
        );
    }

    /**
     * 通过  提交队列后台打款
     */
    public function settlementDeal()
    {
        $this->result($this->logicBalanceSettlement->dealBalanceSettlement(['a.id' => $this->request->param('settlement_id')]));
    }


    /**
     * 驳回申请
     */
    public function settlementRebut()
    {
        $this->result($this->logicBalanceSettlement->rebutBalanceSettlement(['a.id' => $this->request->param('settlement_id')],$this->request->param('reason')));
    }
}
